<?php

namespace addons\shopro\service\user;

use app\admin\model\Children;
use app\admin\model\market\Acceleration;
use app\admin\model\shopro\user\User as UserModel;

class User
{
    /**
     * @name 追加消费金额
     * @param  int|object  $user  会员对象或会员ID
     * @param  float  $amount  变更金额
     * @return boolean
     */
    public static function consume($user, $amount)
    {
        // 判断金额
        if ($amount == 0) {
            return false;
        }

        // 判断用户
        if (is_numeric($user)) {
            $user = UserModel::getById($user['id']);
        }
        if (!$user) {
            error_stop('未找到用户');
        }
        $accelerationModel    = new Acceleration();
        $where['id']          = ['neq', $user['level']];
        $where['performance'] = ['elt', $amount];
        $where['status']      = 1;
        $accelerationInfo     = $accelerationModel->where($where)->order('id desc')->find();
        if ($accelerationInfo) {
            if($user['level'] < $accelerationInfo->id){
                db('user')->where(['id'=>$user['id']])->update(['level'=> $accelerationInfo->id]);
            }
        }
        // 更新会员代金券信息
//        $user->setInc('total_consume', $amount);
        //判断是否能升级
        return true;
    }
}
